<template>
	<view class="container">
		<view class="top">
			<image src="../../../static/image/login/logo.png" mode=""></image>
		</view>
		<view class="welcome">
			欢迎登录
		</view>
		<view class="login">
			<form @submit.prevent="submit">
				<view class="items">
					<view class="item_table">
						账 &nbsp;&nbsp;号
					</view>
					<view class="item_input">
						<input class="inputs_item" v-model="formData.loginName" auto-focus placeholder=" 请输入您的账号"
							placeholder-class='placeholder' />
					</view>
				</view>

				<view class="items">
					<view class="item_table">
						密 &nbsp;&nbsp;码
					</view>
					<view class="item_input">
						<input class="inputs_item" type="password" v-model="formData.password" auto-focus placeholder=" 请输入您的密码"
							placeholder-class='placeholder' />
					</view>
				</view>
			</form>
		</view>
		<view class="logon" @click="submit">登&nbsp;&nbsp;&nbsp;录</view>
	</view>
</template>

<script>
	import $http from '@/ajax/http.js';
	import JSEncrypt from '@/static/js/jsencrypt.min.js'
	export default {
		data() {
			return {
				formData: {
					loginName: 'test',
					password: '123456',
				},
				// 门店合伙人
				storeRole: '6',
				// 服务合伙人
				serveRole: '7',
				// 业务合伙人
				business: '5',
				// 城市合伙人
				cityRole:'4',
				// 事业合伙人
				cause:'3'
			}
		},
		created() {
			// 检查本地是否存在 token
			const token = wx.getStorageSync('token');

			if (token) {
				// 如果本地已有 token，直接跳转到主页或执行其他操作
				wx.switchTab({
					url: '/pages/index/index',
				});
			}
		},
		methods: {
			submit() {
				const publicKey = 'MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBANL378k3RiZHWx5AfJqdH9xRNBmD9wGD\n' +
					'2iRe41HdTNF8RUhNnHit5NpMNtGL0NPTSSpPjjI1kJfVorRvaQerUgkCAwEAAQ=='
				const encryptor = new JSEncrypt()
				encryptor.setPublicKey(publicKey)

				const username = this.formData.loginName
				const passwords = this.formData.password

				const password = encryptor.encrypt(passwords);

				$http.request({
					url: '/common/weichat/auth/accountLogin',
					data: {
						username: username,
						password: password
					},
					method: 'POST',
				}).then((res) => {
					console.log(res, "登录成功参数");
					wx.setStorageSync("token", res.token)
					wx.setStorageSync("roles", res.userRole)
					// 门店合伙人
					if (res.code == 200 || res.userRole == this.storeRole) {
						uni.reLaunch({
							url: `/pages/index/index?roles=${this.storeRole}`
						})
						// 服务合伙人
					} else if (res.code == 200 || res.userRole == this.serveRole) {
						uni.reLaunch({
							url: `/pages/index/index?role=${this.serveRole}`
						})
						// 业务合伙人
					} else if (res.code == 200 || res.userRole == this.business) {
						uni.reLaunch({
							url: `/pages/index/index?role=${this.business}`
						})
						wx.setStorageSync("username",this.formData.loginName)
						// 城市合伙人
					}else if(res.code == 200 || res.userRole == this.cityRole) {
						uni.reLaunch({
							url: `/pages/index/index?role=${this.cityRole}`
						})
					}else if(res.code == 200 || res.userRole == this.cause) {
						uni.reLaunch({
							url: `/pages/index/index?role=${this.cause}`
						})
					}
				}).catch((error) => {
					wx.showModal({
						title:'登陆失败',
						content: '您的账号/密码输入错误，请重新输入！',
						showCancel:false
					});
				})
			}

		}
	}
</script>

<style scoped lang="scss">
	.container {
		display: flex;
		align-items: center;
		flex-direction: column;
		height: 100vh;
		background: linear-gradient(to top, RGBA(0, 74, 75, 1), RGBA(19, 149, 162, 1));

		.top {
			width: 177rpx;
			height: 113rpx;

			image {
				width: 177rpx;
				height: 113rpx;
				margin-top: 150rpx;
			}
		}

		.welcome {
			font-size: 40rpx;
			font-family: MiSans;
			font-weight: 400;
			color: #FFFFFF;
			width: 640rpx;
			margin-top: 280rpx;
		}

		.login {
			margin-top: 73rpx;
			width: 640rpx;

			.items {
				display: flex;
				flex-direction: row;
				align-items: center;
				width: 640rpx;
				height: 80rpx;
				line-height: 80rpx;
				background: url('../../../static/image/login/bj1.png');
				background-size: cover;
				border-radius: 20rpx;
				margin-bottom: 37rpx;

				.item_table {
					width: 184rpx;
					height: 81rpx;
					font-size: 32rpx;
					font-weight: 400;
					color: #FFFFFF;
					text-align: center;
				}
			}
		}

		.logon {
			display: flex;
			align-items: center;
			justify-content: center;
			width: 640rpx;
			height: 91rpx;
			margin-top: 40rpx;
			background: linear-gradient(to right top, rgba(19, 182, 162, 1.0), rgba(0, 131, 141, 1.0));
			border-radius: 20rpx;
			color: #FFFFFF;
			font-size: 35rpx;
			box-shadow: 0px 3px 3px #024547;
		}
	}
</style>